<?php

/**
 * 金果统计 每天00:05分运行一次 统计金果数量
 *
 * @author zhoujf
 */
require_once dirname(__DIR__) . "/cronBase.php";
require_once(ROOT_PATH . 'framework/config/mobileapi/base.cfg.php');

use Common\Bridge;
use Center\CoinLog as MCoinLog;

class coinLog extends cronBase
{
    public function doDefault()
    {
        global $argv;
        //计算昨天的时间
        $pre_day = intval($argv[1]) + 1;
        $date = date('Y-m-d', time() - 86400 * $pre_day);
        $start = date('Y-m-d 00:00:00', strtotime($date));
        $end = date('Y-m-d 23:59:59', strtotime($date));

        //金果数量统计
        // 总
        $select_sql = "SELECT SUM(`CoinNum`) AS Total,`Type` FROM `CoinLog` WHERE CreateTime >= '" . $start . "' AND `CreateTime` <= '" . $end . "' GROUP BY `Type`";
        $type_num = $this->centerQuery($select_sql);
        $rows = $type_list = $type_nums = [];
        foreach ($type_num as $v) {
            $type_nums[$v['Type']] = $v['Total'];
        }
        // ios
        $select_sql = "SELECT SUM(`CoinNum`) AS Total,`Type` FROM `CoinLog` WHERE AppId = 10 AND CreateTime >= '" . $start . "' AND `CreateTime` <= '" . $end . "' GROUP BY `Type`";
        $type_num2 = $this->centerQuery($select_sql);
        $rows2 = $type_list2 = $type_nums2 = [];
        foreach ($type_num2 as $v) {
            $type_nums2[$v['Type']] = $v['Total'];
        }
        // android
        $select_sql = "SELECT SUM(`CoinNum`) AS Total,`Type` FROM `CoinLog` WHERE AppId = 50 AND CreateTime >= '" . $start . "' AND `CreateTime` <= '" . $end . "' GROUP BY `Type`";
        $type_num3 = $this->centerQuery($select_sql);
        $rows3 = $type_list3 = $type_nums3 = [];
        foreach ($type_num3 as $v) {
            $type_nums3[$v['Type']] = $v['Total'];
        }

        $types = MCoinLog::TYPE_LIST;
        foreach ($types as $type => $desc) {
            if (isset($type_nums[$type])) {
                $type_list[$type] = $type_nums[$type];
            } else {
                $type_list[$type] = 0;
            }
            if (isset($type_nums2[$type])) {
                $type_list2[$type] = $type_nums2[$type];
            } else {
                $type_list2[$type] = 0;
            }
            if (isset($type_nums3[$type])) {
                $type_list3[$type] = $type_nums3[$type];
            } else {
                $type_list3[$type] = 0;
            }
        }
        foreach ($type_list as $k => $v) {
            $rows[] = [
                'Type' => 'coin_type_' . $k,
                'Num' => $v,
                'CreateDate' => $date,
            ];
        }
        foreach ($type_list2 as $k => $v) {
            $rows2[] = [
                'Type' => 'coin_type_ios_' . $k,
                'Num' => $v,
                'CreateDate' => $date,
            ];
        }
        foreach ($type_list3 as $k => $v) {
            $rows3[] = [
                'Type' => 'coin_type_adr_' . $k,
                'Num' => $v,
                'CreateDate' => $date,
            ];
        }
        //写入数据
        $pdo = Bridge::pdo(Bridge::DB_ADMIN_W);
        $pdo->insert('SiteStatus', $rows, ['ignore' => true]);
        $pdo->insert('SiteStatus', $rows2, ['ignore' => true]);
        $pdo->insert('SiteStatus', $rows3, ['ignore' => true]);
        $pdo->close();
        Bridge::pdo(Bridge::DB_ADMIN_W, 'force_close');
        $pdo = null;
    }

    /**
     * 查询语句
     *
     * @param string $sql 查询的SQL语句
     * @return array
     */
    private function centerQuery($sql)
    {
        $pdo = Bridge::pdo(Bridge::DB_CENTER_R);
        $obj = $pdo->query($sql);

        return $obj->fetchAll();
    }
}

$app->run();